package com.space.school.dao.impl;

import com.space.school.dao.UserDao;
import com.space.school.domain.User;
import com.space.school.utils.SpaceJDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;

public class UserDaoImpl implements UserDao {
    //    获取数据库并连接
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(SpaceJDBCUtils.getDs());

    @Override
    public List<User> search(int start, int pageSize, Map<String, String[]> map) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("select * from user where 1=1 ");
        ArrayList<Object> params = new ArrayList<>();
//        取map键中的数组
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (!map.get(key)[0].equals("") && !key.equals("currentPage")) {
                stringBuffer.append("and " + key + " like ?");
                params.add("%" + map.get(key)[0] + "%");
            }
        }
        params.add(start);
        params.add(pageSize);
        stringBuffer.append(" order by userId  limit ?,? ");
        return jdbcTemplate.query(stringBuffer.toString(), new BeanPropertyRowMapper<User>(User.class),params.toArray());
    }

    @Override
    public int count(Map<String,String[]> map) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("select count(*) from user where 1=1 ");
        ArrayList<Object> params = new ArrayList<>();
//        取map键中的数组
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (!map.get(key)[0].equals("")&& !key.equals("currentPage")) {
                stringBuffer.append("and " + key + " like ?");
                params.add("%" + map.get(key)[0] + "%");
            }
        }
        return jdbcTemplate.queryForObject(stringBuffer.toString(), Integer.class,params.toArray());
    }

    @Override
    public int delete(int id) {
        // 登录查询
        String sql = "delete from user where userId=?";
        int result = jdbcTemplate.update(sql, id);
        return result;
    }

    @Override
    public void update(User user) {
        // 更新语句
        String sql = "UPDATE user SET password = ?,updateTime=? WHERE userId = ? ";
        jdbcTemplate.update(sql, user.getPassWord(), new Date(), user.getUserId());
    }

    @Override
    public User findById(int id) {
        String sql = "select * from user where userId=?";
        List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class), id);
        if (users.size() > 0) {
            return users.get(0);
        }
        return null;
     }

    @Override
    public void updateStatus(int id, String status) {
        // 更新语句
        String sql = "UPDATE user SET status = ?,updateTime=? WHERE userId = ? ";
        jdbcTemplate.update(sql, status, new Date(), id);
    }

    @Override
    public void save(User user) {
        // 新增语句
        String sql = "insert into user (username,password,createTime) values(?,?,?)";
        jdbcTemplate.update(sql, user.getUserName(), user.getPassWord(), new Date());
    }
}
